//my glue package
package glue;

import java.io.*;
import java.lang.*;
import java.util.*;


public class RecentProjects implements Serializable 
{
	ArrayList<String> projectName = null;
	HashMap<String,File> projectPath = null;
	int listsize = 4;
	
	
	public RecentProjects()
	{
		projectName = new ArrayList<String>();
		projectPath = new HashMap<String, File>();
	}
	
	public RecentProjects(int listsize)
	{
		this.listsize = listsize;
		projectName = new ArrayList<String>();
		projectPath = new HashMap<String, File>();
	}
	
	public void addProject(String name, String path){
		File f = new File(path);
		addProject(name, f);
	}
	
	public void addProject(String name, File path){
		if(path.exists()){
			removeProject(name);
			if(projectName.size() >= listsize){
				projectPath.remove(projectName.get(projectName.size() - 1));
				projectName.remove(projectName.size() - 1);
				projectName.trimToSize();
			}
			projectName.add(0, name);
			projectPath.put(name, path);
		}else{
		}
	}
	

	public void removeProject(String name){
		if(projectName.indexOf(name) != -1)
		{
			projectPath.remove(name);
			projectName.remove(name);
			projectName.trimToSize();
		}
	}
	
	public Object[] getProjectNames(){
		Object [] list = projectName.toArray();
		for(Object o : list){
			File f = getProjectPath(o.toString());
			if(!f.exists()){
				removeProject(o.toString());
			}
		}
		return projectName.toArray();
	}
	
	public File getProjectPath(String projectName){
		return projectPath.get(projectName);
	}
	
	
	static public RecentProjects open(){
		return open(new String("recentprojects.rpx"));//recent projects list
	}
	
	static public RecentProjects open(String path){
		RecentProjects p = null;
    	try{
	    	ObjectInputStream s = new ObjectInputStream(new FileInputStream(path));
	    	p = (RecentProjects)s.readObject();
	    	s.close();
		}catch(IOException io){
			p = new RecentProjects();
			RecentProjects.save(p);
		}catch(ClassNotFoundException cnf){
			System.err.print("recentproject dne: " + cnf.getMessage());
		}
		return p;
	}
	
	public void save(){
		save(new String("recentprojects.rpl"));
	}
	
	public void save(String fileName){
    	try{
	    	ObjectOutputStream s = new ObjectOutputStream(new FileOutputStream(fileName));
	    	s.writeObject(this);
	    	s.close();
		}catch(IOException io){
			System.err.print("My exception thrown(Recent project): " + io.getMessage());
		}
	}
	
    static public void save(RecentProjects p){
	    save(p, new String("recentprojects.rpx"));
    }   

    static public void save(RecentProjects p, String fileName){
	    if(p != null){
	    	try{
		    	ObjectOutputStream s = new ObjectOutputStream(new FileOutputStream(fileName));
		    	s.writeObject(p);
		    	s.close();
			}catch(IOException io){
				System.err.print("My exception thrown(Recent project): " + io.getMessage());
			}
	    }
    }   	  
}

